package com.example.emergencyattempt01.fragement.administrator_fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.emergencyattempt01.R;
import com.example.emergencyattempt01.activity.utils.NetWorkUtils;
import com.example.emergencyattempt01.activity.utils.Pack_DePackUtil;
import com.example.emergencyattempt01.activity.utils.ServerMessage;
import com.example.emergencyattempt01.adapter.Ad_CheckingAdapter;
import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Administrator_Checking extends Fragment {

    ListView ad_checkingListView;
    List<Map<String,Object>> ad_checkingList=new ArrayList<>();
    Ad_CheckingAdapter checkingAdapter;

    Handler handlerForAdChecking=new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what)
            {
                case LOAD_CHECKING_SUCCESS:
                    Toast.makeText(getActivity(),"当前有"+ad_checkingList.size()+"条申请待处理",Toast.LENGTH_LONG).show();
                    checkingAdapter.notifyDataSetChanged();
                    break;
                case LOAD_CHECKING_NOT_FOUND:
                    Toast.makeText(getActivity(),"当前没有任何请求",Toast.LENGTH_SHORT).show();
                    checkingAdapter.notifyDataSetChanged();
                    break;
                case LOAD_CHECKING_FAIL:
                    Toast.makeText(getActivity(),"加载失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    final int LOAD_CHECKING_SUCCESS=0x025;
    final int LOAD_CHECKING_NOT_FOUND=0x026;
    final int LOAD_CHECKING_FAIL=0x027;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.administrator_checking_layout,null);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 初始化界面
        inintPage();
        // 设置加载
        setLoad();
    }



    private void setLoad() {
     new Thread()
     {
         @Override
         public void run() {
             try
             {
                 ServerMessage serverMessage=NetWorkUtils.getInstance().create(null,"/find_requesting");
                 int code=serverMessage.getRespondCode();
                 if(code==200)
                 {
                     String fromServer=(String)serverMessage.getFromServer();
                     if(Pack_DePackUtil.dePackBorrowAhead(fromServer,ad_checkingList))
                     {
                         Message message=new Message();
                         message.what=LOAD_CHECKING_SUCCESS;
                         handlerForAdChecking.sendMessage(message);
                     }
                     else {
                         Message message=new Message();
                         message.what=LOAD_CHECKING_NOT_FOUND;
                         handlerForAdChecking.sendMessage(message);
                     }
                 }
                 else {
                     Message message=new Message();
                     message.what=LOAD_CHECKING_FAIL;
                     handlerForAdChecking.sendMessage(message);
                 }

             }catch (JSONException e){e.printStackTrace();}
             catch (IOException e){e.printStackTrace();
                 Message message=new Message();
                 message.what=LOAD_CHECKING_FAIL;
                 handlerForAdChecking.sendMessage(message);}
         }
     }.start();
    }

    private void inintPage() {
        ad_checkingListView=getActivity().findViewById(R.id.ad_checking_listView);
        checkingAdapter=new Ad_CheckingAdapter(getActivity(),ad_checkingList);
        ad_checkingListView.setAdapter(checkingAdapter);
    }

}
